package com.ssm.mssm.function.impl;

import com.ssm.common.*;
import com.ssm.mssm.dao.InformationDao;
import com.ssm.mssm.entity.InformationEntity;
import com.ssm.mssm.function.InformationFunction;
import com.ssm.mssm.web.handler.ao.InformationAo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ssm.common.PasswordUtil.generate;
import static com.ssm.common.PasswordUtil.verify;
import static com.ssm.common.ThisSystemUtil.*;

/**
 * Created by asus on 2017/7/14.
 */
@Service
public class InformationFunctionImpl implements InformationFunction{

    @Autowired
    InformationDao idao;

    @Override
    public List<InformationEntity> queryInformation(String username) throws Exception {
        int juese;
        if("管理员".equals(username)){
            juese=1;
        }else{
            juese=0;
        }
        List<InformationEntity> list=idao.selectList("juese",juese);
        return list;
    }

    @Override
    public InformationEntity addInformation(InformationAo ia) throws Exception {
        if(ia==null){
            throw new IllegalArgumentException("ia不能为空");
        }
        String username=$("用户名必须填写",ia.getUsername());
        String account=$("账号必须填写",ia.getAccount());
        String password=$("密码必须填写",ia.getPassword());
        String newpassword=$("请重新填写密码",ia.getNewpassword());
        assertEquals("两次密码不一致",ia.getPassword(),ia.getNewpassword());
        String sex=$("性别必须填写",ia.getSex());
        assertFalse("用户名重复",idao.exsits("username",username));
        InformationEntity i=new InformationEntity();
        i.setId(ThisSystemUtil2.uuid());
        i.setUsername(ia.getUsername());
        if("1".equals(ia.getSex())){
            i.setSex("男");
        }else{
            i.setSex("女");
        }
        i.setQq(ia.getQq());
        i.setEmail(ia.getEmail());
        i.setWechat(ia.getWechat());
        if(ia.getCharacter().equals("1")){
            i.setCharacter(1);
        }else{
            i.setCharacter(0);
        }
        i.setAccount(ia.getAccount());
        i.setPassword(generate(ia.getPassword()));
        idao.insert(i);
        return i;
    }

    @Override
    public InformationEntity delectInformation(String id) throws Exception {
        id=$("ID必须填写",id);
        InformationEntity i=idao.select("id",id);
        if(i==null){
            throw new ThisSystemException("没有此用户");
        }
        idao.delete("id",id);
        return i;
    }

    @Override
    public InformationEntity updateInformation(String id,InformationAo ia) throws Exception {
        if(ia==null){
            throw new IllegalArgumentException("ia不能为空");
        }
        String username=$("用户名必须填写",ia.getUsername());
        String password=$("密码必须填写",ia.getPassword());
        String newpassword=$("密码必须填写",ia.getNewpassword());
        assertEquals("两次密码不一样",ia.getPassword(),ia.getNewpassword());

        InformationEntity i=new InformationEntity();
        i.setId(id);
        i.setUsername(ia.getUsername());
        if("1".equals(ia.getSex())){
            i.setSex("男");
        }else{
            i.setSex("女");
        }
        i.setSex(ia.getSex());
        i.setQq(ia.getQq());
        i.setEmail(ia.getEmail());
        i.setWechat(ia.getWechat());
        if(ia.getCharacter().equals("1")){
            i.setCharacter(1);
        }else{
            i.setCharacter(0);
        }
        //i.setPassword(ia.getPassword());
        i.setPassword(generate(ia.getPassword()));
        System.out.println("password"+ia.getPassword());
        idao.update(i);
        return i;
    }

    @Override
    public InformationEntity showInformation(String id) throws Exception {
        InformationEntity i=idao.select("id",id);
        return i;
    }

    @Override
    public List<InformationEntity> selectAllInformation() throws Exception {

        List<InformationEntity> lstUsers = idao.selectAllInformation();
        return lstUsers;
    }

    @Override
    public List<InformationEntity> selectLikeInformation(String string) throws Exception {
        List<InformationEntity> slist=idao.selectLike(string);
        return slist;
    }

    @Override
    public InformationEntity login(String account, String password) throws Exception {
        account= ThisSystemUtil.throwIfBlank("账户不能为空",account);
        password= ThisSystemUtil.throwIfBlank("密码不能为空",password);

        InformationEntity i=idao.select("account",account);
        System.out.println("password"+i.getPassword());
        if(i==null){
            throw new ThisSystemException("账户不存在");
        }
        if(!PasswordUtil.verify(password,i.getPassword())){
            throw new ThisSystemException("密码错误");
        }
        return i;
    }

    @Override
    public InformationEntity select(String id) throws Exception {
        InformationEntity i=idao.select("id",id);
        return  i;
    }

    @Override
    public InformationEntity updatePassword(String id, InformationEntity ie) throws Exception {
        idao.update(ie);
        return null;
    }
}
